/**
 * @author Stephan Pfaff (webmaster@derdaumen.net)
 */

/**
 * The Button Object
 * @param vars object providing the button properties (loaded from the database)
 * @return
 */
function Button(vars, params, parent){
	if(vars != null) {
		try {
			Item.call(this, vars.type, vars.basename, vars.path, vars.prefix);
			this.id 			= vars.id;
			this.category_id 	= vars.category_id;
			this.project_id 	= vars.project_id;
			this.alias 			= vars.alias;
			this.label 			= vars.label;
			this.thumb 			= vars.thumb;
			this.thumb_enable 	= new Boolean(vars.thumb_enable);
			this.img 			= vars.img;
			this.img_enable 	= new Boolean(vars.img_enable);
			this.x				= vars.x*site.gridSize;
			this.y				= vars.y*site.gridSize;
			this.content_x		= vars.content_x*site.gridSize;
			this.content_y		= vars.content_y*site.gridSize;
			this.content_width	= (vars.content_width*site.gridSize)-7;
			this.content_height	= (vars.content_height*site.gridSize)-7;
			this.interactive	= new Boolean(vars.interactive);
			this.caption			= vars.text;
			this.caption_enable	= new Boolean(vars.text_enable);
			this.link			= vars.link;
			this.link_enable	= new Boolean(vars.link_enable);
			this.img_delete 	= false;

			this.div_id = this.prefix+"_"+this.id;
			this.div_class = this.interactive == true ? this.prefix : this.prefix+" active";
			this.div_x = this.interactive == true ? "left: "+this.x+"px;" : "left: "+this.content_x+"px;";
			this.div_y = this.interactive == true ? "top: "+this.y+"px;" : "top: "+this.content_y+"px;";
			this.div_width = this.interactive == true ? "" : "width: "+this.content_width+"px;";
			this.div_height = this.interactive == true ? "" : " height: "+this.content_height+"px;";
			this.bg_active = '#232323';
			if(this.label != "" || this.thumb != "")
				this.bg_inactive = '#232323';
			else
				this.bg_inactive = '#999';
			this.bg = this.interactive == true ? this.bg_inactive : this.bg_active;
			
			if(noPHPMode) {
				var html = ["<div id='"+this.div_id+"' class='item "+this.div_class+"'"
					+"style='"+this.div_x+" "+this.div_y+" "+this.div_width+" "+this.div_height+" background:"+this.bg+";'>\n"];
					if(this.link != null && this.link_enable == true) html.push("	<a id='outerlink_"+this.div_id+"' class='link outer' href='"+this.link+"' target='_blank'></a>\n");
					if(this.label != "") html.push("	<div id='label_"+this.div_id+"' class='label'>"+this.label+"</div>\n");
					if(this.thumb != "" && this.thumb_enable  == true) html.push("	<img id='thumb_"+this.div_id+"' class='thumb' src='"+this.thumb+"' alt='"+this.alias+"' />\n");
					if(this.caption != "" && this.caption_enable  == true) html.push("	<div id='caption_"+this.div_id+"' class='caption''>\n<div id='captText_"+this.div_id+"' class='captText'>"+this.caption+"</div>\n</div>\n");
					if(this.img != "" && this.img_enable  == true) html.push("	<a href='detail.jpg' rel='shadowbox["+this.alias+"]'><img id='img_"+this.div_id+"' class='img' src='"+this.img+"' alt='"+this.alias+"' width='"+this.content_width+"' height='"+this.content_height+"' /></a>\n");
					html.push("</div>\n");
					this.$div = $(html.join(''));
					parent.$div.append(this.$div);
			} else {
				this.$div = $("#"+this.prefix+"_"+this.id);
			}
			if(noPHPMode && allowDraggable) {
				this.$div.draggable({
					revert: false,
					cancel: '.active',
					zIndex: 100
				});
			}
			//durch animationen ersetzte lins zum umschalten der buttons 
			//in der php Version entfernen
			$("#"+this.div_id+" #innerlink_"+this.div_id).remove();
			
			var handle = this;
			//hover
			this.$div.hover(function(event) {
				handle.activate($(this), event);
			},
			function(event) {
				handle.deactivate($(this), event);
			});
			//click
			this.$div.click(function(event) {
				handle.clickHandler($(this), event);
			});
		} catch (e) {
			if(debug) alert('Initialization of Button failed, missing or wrong parameter! ('+e+')');
		}
	}
};
Button.prototype = new Item();
Button.prototype.id = 0;
Button.prototype.category_id = 0;
Button.prototype.project_id = 0;
Button.prototype.label = "";
Button.prototype.alias = "";
Button.prototype.x = 0;
Button.prototype.y = 0;
Button.prototype.content_x = 0;
Button.prototype.content_y = 0;
Button.prototype.content_width = 1;
Button.prototype.content_height = 1;
Button.prototype.interactive = 0;
Button.prototype.link = "";
Button.prototype.link_enable = false;
Button.prototype.caption = "";
Button.prototype.caption_enable = false;
Button.prototype.thumb = "";
Button.prototype.thumb_enable = false;
Button.prototype.img = "";
Button.prototype.img_enable = false;
Button.prototype.img_delete = false;
Button.prototype.html = "";
Button.prototype.$div = null;
Button.prototype.div_id = null;
Button.prototype.div_class = null;
Button.prototype.div_x = null;
Button.prototype.div_y = null;
Button.prototype.div_width = null;
Button.prototype.div_height = null;
Button.prototype.bg_active = null;
Button.prototype.bg_inactive = null;
Button.prototype.bg = null;

Button.prototype.activate = function($div, event) {
	var div_id = this.div_id;
	if(!this.locked && this.interactive == true) {
		$div.clearQueue()
		.stop()
		.css('z-index', 99)
		.queue(function() {
			var $label = $("#label_"+div_id);
			var $thumb = $("#thumb_"+div_id);
			if($label.length) {
				$thumb.fadeOut(site.btnAnimationDuration);
				$label.fadeOut(site.btnAnimationDuration, function() {
					$div.dequeue();
				});
			} else if($thumb.length) {
				$label.fadeOut(site.btnAnimationDuration);
				$thumb.fadeOut(site.btnAnimationDuration, function() {
					$div.dequeue();
				});
			} else {
				$div.dequeue();
			}
		})
		.animate({
			backgroundColor: this.bg_active,
			left: this.content_x,
			top: this.content_y,
			width: this.content_width,
			height: this.content_height
		}, site.btnAnimationDuration, function() {
			$("#img_"+div_id).fadeIn(site.btnAnimationDuration);
			$("#caption_"+div_id).fadeIn(site.btnAnimationDuration);
			$div.addClass("active");
		});
	}
};
Button.prototype.deactivate = function($div, event) {
	var div_id = this.div_id;
	if(!this.locked && this.interactive == true) {
		$div.clearQueue()
		.stop()
		.css('z-index', 98)
		.queue(function() {
			var $img = $("#img_"+div_id);
			var $caption = $("#caption_"+div_id);
			if($img.length) {
				$caption.fadeOut(site.btnAnimationDuration/2);
				$img.fadeOut(site.btnAnimationDuration/2, function() {
					$div.dequeue();
				});
			} else if($caption.length) {
				$img.fadeOut(site.btnAnimationDuration);
				$caption.fadeOut(site.btnAnimationDuration, function() {
					$div.dequeue();
				});
			} else {
				$div.dequeue();
			}
		})
		.animate({
			backgroundColor: this.bg_inactive,
			left: this.x,
			top: this.y,
			width: 73,
			height: 73
		}, site.btnAnimationDuration, function() {
			$div.css('z-index', 0);
			$("#label_"+div_id).fadeIn(site.btnAnimationDuration);
			$("#thumb_"+div_id).fadeIn(site.btnAnimationDuration);
			$div.removeClass("active");
		});
	}
};
Button.prototype.clickHandler = function($div, event) {
	/*if($div.hasClass('active')) {
		this.deactivate($div, event);
	} else {
		this.activate($div, event);
	}*/
};